<?php
define('START_TIME', microtime(true));
define('START_MEMORY_USAGE', memory_get_usage());
define('NOW', time());
define('DATA_PATH', ROOT . 'data' . DS);
spl_autoload_register(array('JBlog', 'autoload'));

class JBlog {
	const VERSION = '2.0'; //版本号
	const RELEASE = '20120501'; //发布日期
	const CHARSET = 'UTF-8'; //编码
	const DBUG = true; //是否调试模式
	
	private static $_instance;
	private $_config;
	private $_request;
	private $_view;
	
	public function __construct(Config $appConf) {
		$this->_config = $appConf;
		$this->_request = new Request();
		$this->_view = new View_Simple();
		$this->_view->setPath(ROOT.'theme'.DS);
		self::$_instance = $this;
	}
	
	public function getConfig() {
		return $this->_config;
	}
	
	/**
	 * autoload方法
	 * 
	 * @param string $class 
	 */
	public static function autoload($class) {
		$class = preg_replace('/([^a-z0-9\_])/i', '', $class);
		$class = str_replace('_', DS, $class);
		if (is_file(SYS_PATH."Core/{$class}.php")) {
			include SYS_PATH."Core/{$class}.php";
		} elseif (strpos($class, 'Controller') !== false) {
			$filename = SYS_PATH . 'Module/'.MODULE_NAME.'/'.substr($class, 0, -10).'.php';
			if (is_file($filename)) include $filename;
		} elseif (is_file(SYS_PATH."Model/{$class}.php")) {
			include SYS_PATH . "Model/{$class}.php";
		} else {
			$filename = SYS_PATH."Lib/{$class}.php";
			if (is_file($filename)) include $filename;
		}
	}	
	
	public function run() {
		$dispConf = $this->_config->dispatcher;
		$dispatcher = Dispatcher::getInstance();
		$dispatcher->setDefaultModule(MODULE_NAME);
		$dispatcher->setDefaultController($dispConf->defaultController);
		$dispatcher->setDefaultAction($dispConf->defaultAction);
		$dispatcher->setRequest($this->_request);
		$dispatcher->setView($this->_view);
		$dispatcher->dispatch();
	}
}







